<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 公共功能管理
 */
namespace app\admin\model;

use app\admin\model\Model;

class Common extends Model{

    /**
     * icon列表
     * @param array $iconlist
     * @param array $where
     */
    public function iconlist($where='')
    {
        //icon数据
        $datalist = Model::name('iconfont')
            ->where($where)
            ->order('id asc')
            ->select()
            ->toArray();
        return $datalist;
    }

    /**
     * 功能列表
     * @param array $subfunctionlist
     * @param array $where
     */
    public function subfunctionlist($where='')
    {
        //icon数据
        $datalist = Model::name('sys_rules')
            ->where($where)
            ->order('id asc')
            ->select()
            ->toArray();
        return $datalist;
    }

    /**
     * 獲取系统基本信息
     * @param array $getbasics 
     * @param array $where 
    */ 
    public function getbasics($where=null,$file='*'){
        //基础数据
        $basics = Model::name('sys_basics')
            ->field($file)
            ->where($where)
            ->find();
        return $basics;
    }

    /**
     * 获取基础信息
     * @param array $getBaseinfo 
     * @param array $where 
    */ 
    public function getBaseinfo($where=null,$file='*'){ 
        $baseinfo = Model::name('sys_basics')
            ->field($file)
            ->where($where)
            ->find();
        $baseinfo['content']=!empty($baseinfo['content'])?json_decode($baseinfo['content'],true):[];
        return $baseinfo;
    }

    /**
     * 角色标识列表
     * @param array $identlist
     * @param array $where 
     */
    public function identlist($where='dels < 1',$file='*')
    {
        
        //部门数据
        $identlist = Model::name('sys_ident')
            ->field($file)
            ->where($where)
            ->order('id asc')
            ->select()
            ->toArray();
        return $identlist;
    }

    /**
     * 广告类别选择
     * @param array $area 
     * @param array $where 
    */ 
    public function advertclass($where=null){

        $advertclass = Model::name('web_advertclass')
            ->where($where)
            ->select()
            ->toArray();
        return $advertclass;
    }

    /**
     * 菜單列表選擇
     * @param array $getmenu 
     * @param array $where 
    */ 
    public function getmenu($where=null,$file='*'){
        $menu = Model::name('web_menu')
            ->field($file)
            ->where($where)
            ->order('orderid asc')
            ->select()
            ->toArray();
        return $menu;
    }

    /**
     * 獲取單項類別信息
     * @param array $getsingle 
     * @param array $where 
    */ 
    public function getsingle($where=null,$file='*'){ 
        $singleclass = Model::name('web_single')
            ->field($file)
            ->where($where)
            ->select()
            ->toArray();
        return $singleclass;
    }

    /**
     * 获取基础语言列表
     * @param array $getBase 
     * @param array $where 
    */ 
    public function getLang(){ 
        $lang=[];
        $langs=[
            'list'=>[],
            'total'=>0
        ];
        $lang = Model::name('sys_basics')
            ->field('content,notes')
            ->where('keyname="lang"')
            ->find();
        if(!empty($lang['content'])){
            $lang['notes'] = json_decode($lang['notes'],true);
            $lang['content'] = json_decode($lang['content'],true);
            foreach ($lang['content'] as $key => $value) {
                $langs['list'][$key]['value']=$value;
                $langs['list'][$key]['name']=$lang['notes'][$key];
            }
            unset($lang['content'],$lang['notes']);
        }else{
            $langs['list']=config('langs');
        }
        $langs['total']=count($langs['list']);
        return $langs;
    }

    /**
     * 验证验证
     * @param array $getcode 
     * @param array $where 
    */ 
    public function getcode($code,$where=''){
        $id = Model::name('user_code')
            ->where('code="'.$code.'" and endtime>'.time().$where)
            ->find();
        return $id;
    }
    
    /**
     * 保存验证码
     * @param array $savacode 
     * @param array $where 
    */ 
    public function savacode($where=0,$data=[]){
        $num=Model::name('user_code')
            ->where($where)
            ->count('id');
        switch (true) {
            case $num>0:
                Model::name('user_code')
                    ->where($where)
                    ->update($data);
                break;
            
            default:
                Model::name('user_code')
                    ->insert($data);
                break;
        }
        return true;
    }

}